/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.front.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.front.entity.FrontUser;
import com.thinkgem.jeesite.modules.front.service.FrontUserService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;

/**
 * 用户管理Controller
 * @author 朱俊吉
 * @version 2017-11-25
 */
@Controller
@RequestMapping(value = "${adminPath}/front/frontUser")
public class FrontUserController extends BaseController {

	@Autowired
	private FrontUserService frontUserService;
	
	/**
	 * 验证登录名是否有效
	 * @param oldLoginName
	 * @param loginName
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("front:frontUser:edit")
	@RequestMapping(value = "checkLoginName")
	public String checkLoginName(String oldAccount, String account) {
		if (account !=null && account.equals(oldAccount)) {
			return "true";
		} else if (account !=null && frontUserService.getUserByAccount(account) == null) {
			return "true";
		}
		return "false";
	}
	
	@ModelAttribute
	public FrontUser get(@RequestParam(required=false) String id) {
		FrontUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = frontUserService.get(id);
		}
		if (entity == null){
			entity = new FrontUser();
		}
		return entity;
	}
	
	@RequiresPermissions("front:frontUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(FrontUser frontUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<FrontUser> page = frontUserService.findPage(new Page<FrontUser>(request, response), frontUser); 
		model.addAttribute("page", page);
		return "modules/front/frontUserList";
	}

	@RequiresPermissions("front:frontUser:view")
	@RequestMapping(value = "form")
	public String form(FrontUser frontUser, Model model) {
		model.addAttribute("frontUser", frontUser);
		return "modules/front/frontUserForm";
	}

	@RequiresPermissions("front:frontUser:edit")
	@RequestMapping(value = "save")
	public String save(FrontUser frontUser, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, frontUser)){
			return form(frontUser, model);
		}
		// 如果新密码为空，则不更换密码
		if (StringUtils.isNotBlank(frontUser.getNewPassword())) {
			frontUser.setPassword(SystemService.entryptPassword(frontUser.getNewPassword()));
		}
		if (!"true".equals(checkLoginName(frontUser.getOldAccount(), frontUser.getAccount()))){
			addMessage(model, "保存用户'" + frontUser.getAccount() + "'失败，用户名已存在");
			return form(frontUser, model);
		}
		frontUserService.save(frontUser);
		addMessage(redirectAttributes, "保存用户成功");
		return "redirect:"+Global.getAdminPath()+"/front/frontUser/?repage";
	}
	
	@RequiresPermissions("front:frontUser:edit")
	@RequestMapping(value = "delete")
	public String delete(FrontUser frontUser, RedirectAttributes redirectAttributes) {
		frontUserService.delete(frontUser);
		addMessage(redirectAttributes, "删除用户成功");
		return "redirect:"+Global.getAdminPath()+"/front/frontUser/?repage";
	}

}